package com.miao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.miao.pojo.CourseTeacher;
import com.miao.service.ICourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/content/courseTeacher")
public class TeacherController {
    @Autowired
    private ICourseTeacherService iCourseTeacherService;

    //获取师资信息
    @GetMapping("/list/{id}")
    public List<CourseTeacher> getTeacherList(@PathVariable Long id){
        LambdaQueryWrapper<CourseTeacher> TeacherQueryWrapper = new LambdaQueryWrapper<>();
        TeacherQueryWrapper.eq(CourseTeacher::getCourseId,id);
        return iCourseTeacherService.list(TeacherQueryWrapper);
    }

    //添加，修改教师
    @PostMapping
    public CourseTeacher addTeacher(@RequestBody CourseTeacher courseTeacher){
        if(courseTeacher.getId() == null){
            //是新建
            courseTeacher.setCreateDate(LocalDateTime.now());
        }
        iCourseTeacherService.saveOrUpdate(courseTeacher);
        return courseTeacher;
    }

    //删除教师信息
    @DeleteMapping("/course/{courseid}/{id}")
    public void deleteTeacher(@PathVariable Long id, @PathVariable Long courseid){
        LambdaQueryWrapper<CourseTeacher> TeacherQueryWrapper = new LambdaQueryWrapper<>();
        TeacherQueryWrapper.eq(CourseTeacher::getId,id).eq(CourseTeacher::getCourseId,courseid);
        iCourseTeacherService.remove(TeacherQueryWrapper);
    }
}
